package com.cyan.tools.base;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.cyan.tools.R;
import com.cyan.tools.util.RxBus;
import com.cyan.tools.widget.ItemDecoration;

import java.util.ArrayList;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;


/**
 * Created by pc on 2017/10/20.
 */

public abstract class BaseListActivity<T extends BaseListPresenter,V extends RecyclerArrayAdapter> extends AppCompatActivity implements BaseListView {
    public boolean is_first = true;
    public boolean is_refresh=false;
    private Observable<String> observable;
    public T mPresenter;
    public V adapter;
    public RecyclerView recyclerView;
    public int limit=15;
    public SwipeRefreshLayout refreshLayout;
    public TextView empty,error;
    public ProgressBar progressBar;
    public Toolbar toolbar;
    public TextView title;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_list);
        observable= RxBus.get().register(this.getClass().getSimpleName(),String.class);
        observable.subscribe(new Consumer<String>() {
            @Override
            public void accept(String str) throws Exception {
                onAccept(str);
            }
        });
        refreshLayout=findViewById(R.id.refresh_layout);
        empty=findViewById(R.id.empty);
        error=findViewById(R.id.error);
        progressBar=findViewById(R.id.progressBar);
        recyclerView=findViewById(R.id.list);
        toolbar=findViewById(R.id.toolbar);
        title=findViewById(R.id.title);
        setup();
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                is_refresh=true;
                fetch("",limit);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new ItemDecoration(this,1));
        recyclerView.setAdapter(adapter);
    }
    protected abstract void setup();

    public void onAccept(String str){
      is_refresh=true;
      fetch("",limit);
    }
    public String getId(){
        return "";
    }
    public void fetch(String id,int limit){
        mPresenter.fetch(id,limit);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.get().unregister(this.getClass().getSimpleName(),observable);
    }

    @Override
    public void showResult(ArrayList data) {
        if(is_first){
            adapter.setMore(R.layout.view_more, new RecyclerArrayAdapter.OnLoadMoreListener() {
                @Override
                public void onLoadMore() {
                    if(adapter.getCount()%limit==0)
                        fetch(getId(),limit);
                    else adapter.stopMore();
                }
            });
            adapter.setError(R.layout.view_more_error).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    adapter.resumeMore();
                }
            });
            adapter.setNoMore(R.layout.view_nomore);
        }
        if(is_refresh){adapter.clear();
        is_refresh=false;}
        recyclerView.setVisibility(View.VISIBLE);
        empty.setVisibility(View.GONE);
        error.setVisibility(View.GONE);
        adapter.addAll(data);
    }

    @Override
    public void showEmpty() {
        if (adapter.getCount() == 0) {
            adapter.clear();
            empty.setVisibility(View.VISIBLE);
        } else adapter.stopMore();
    }

    @Override
    public void showError() {
        if (adapter.getCount() == 0) {
            adapter.clear();
            error.setVisibility(View.VISIBLE);
        } else adapter.pauseMore();
    }

    @Override
    public void toast(String msg) {

    }

    @Override
    public void showProgress() {
       if(is_first){
           error.setVisibility(View.GONE);
           empty.setVisibility(View.GONE);
           progressBar.setVisibility(View.VISIBLE);
           recyclerView.setVisibility(View.GONE);
       }
    }

    @Override
    public void hideProgress() {
         progressBar.setVisibility(View.GONE);
    }

}
